package com.site.action.backend.dining;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.site.dto.dining.NewsSortBizBean;
import com.site.service.dining.NewsSortService;
import com.site.util.PropertyUtils;
import com.site.util.StringUtils;
import com.site.util.base.action.BaseAction;
import com.site.util.base.service.BaseService;

public class NewsSortAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private List<NewsSortBizBean> sortlist;

	private NewsSortService sortservice;

	private NewsSortBizBean sortbean;

	public List<NewsSortBizBean> getSortlist() {
		return sortlist;
	}

	public void setSortlist(List<NewsSortBizBean> sortlist) {
		this.sortlist = sortlist;
	}

	public NewsSortService getSortservice() {
		return sortservice;
	}

	@Resource
	public void setSortservice(NewsSortService sortservice) {
		this.sortservice = sortservice;
	}

	public NewsSortBizBean getSortbean() {
		return sortbean;
	}

	public void setSortbean(NewsSortBizBean sortbean) {
		this.sortbean = sortbean;
	}

	public String getNewsSorts() {
		sortlist = sortservice.getNewsSorts();
		return "getNewsSorts";
	}

	public String getNewsSortAdd() {
		return "getNewsSortAdd";
	}

	public String getNewsSortEdit() {
		if (sortlist != null && !sortlist.isEmpty()) {
			for (NewsSortBizBean item : sortlist) {
				if (null != item && item.getId() != null) {
					NewsSortBizBean bean = sortservice.getEntityById(item
							.getId());
					sortbean = new NewsSortBizBean();
					PropertyUtils.copyNotNullProperties(sortbean, bean);
				}
			}
		}
		return "getNewsSortEdit";
	}

	public String newsSortAdd() {
		Map<String, Object> map = new HashMap<String, Object>();
		if (CheckData(sortbean)) {
			map.put("sortbean", sortbean);
			sortservice.execute(map, BaseService.COMMAND_ADD);
			super.setMessageID("SYS_MSG_00022");
		} else {
			return getNewsSortAdd();
		}
		super.setBackurl(request.getContextPath()
				+ "/newssort/newssortAction!getNewsSorts.do");
		return "toMessage";
	}

	public String newsSortEdit() {
		if (sortbean.getId() != null && CheckData(sortbean)) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("sortbean", sortbean);
			sortservice.execute(map, BaseService.COMMAND_UPDATE);
			super.setMessageID("SYS_MSG_00022");
		} else {
			return getNewsSortEdit();
		}
		super.setBackurl(request.getContextPath()
				+ "/newssort/newssortAction!getNewsSorts.do");
		return "toMessage";
	}

	public String newsSortDelete() {
		if (sortlist != null && !sortlist.isEmpty()) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("request", request);
			for (NewsSortBizBean item : sortlist) {
				if (null != item && item.getId() != null) {
					map.put("sortbean", item);
					sortservice.execute(map, BaseService.COMMAND_DELETE);
				}
			}
			super.setMessageID("SYS_MSG_00022");
		}
		super.setBackurl(request.getContextPath()
				+ "/newssort/newssortAction!getNewsSorts.do");
		return "toMessage";
	}

	private boolean CheckData(NewsSortBizBean bean) {
		if (StringUtils.isEmpty(bean.getName())) {
			addFieldError("name", "分类名称不能为空！");
			return false;
		}
		return true;
	}
}
